package com.sansui.admin.core.ctrl;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sansui.admin.common.constant.Action;
import com.sansui.admin.common.ctrl.BaseCtrl;
import com.sansui.admin.core.groups.UserAdd;
import com.sansui.admin.core.groups.UserUpdate;
import com.sansui.admin.core.model.User;
import com.sansui.admin.core.model.UserAddModel;
import com.sansui.admin.core.model.UserPage;
import com.sansui.admin.core.service.UserService;

@RestController
@RequestMapping("/system/user")
public class UserController extends BaseCtrl {
	@Autowired
	private UserService userService;

	@RequiresPermissions(_USER_ROUTE + Action._LIST)
	@RequestMapping(value = Action._LIST)
	public Object list(HttpServletRequest request, UserPage user) {
		return userService.findTByPage(user);
	}

	@RequiresPermissions(_USER_ROUTE + Action._ADD)
	@RequestMapping(value = Action._ADD)
	public Object add(@Validated({ UserAdd.class }) UserAddModel user, BindingResult bindingResult) {
		return userService.insertUser(user);
	}

	@RequestMapping(value = Action._SPRIMARY)
	public Object selectByPrimaryKey(String id) {
		return userService.selectByPrimaryKey(id);
	}

	@RequiresPermissions(_USER_ROUTE + Action._UPDATE)
	@RequestMapping(value = Action._UPDATE)
	public Object update(@Validated({ UserUpdate.class }) UserAddModel muser, BindingResult bindingResult) {
		return userService.updateUser(muser);
	}

	@RequiresPermissions(_USER_ROUTE + Action._DELETE)
	@RequestMapping(value = Action._DELETE)
	public Object delete(String[] id) {
		return userService.deleteUser(id);

	}

	@RequestMapping(value = "/findUserByUser")
	public Object findUserByUser(User user) {
		return userService.findTByT(user);
	}

	@RequestMapping(value = "/toUpdatePassword")
	public Object toUpdatePassword() {
		return userService.selectByPrimaryKey();
	}

	@RequestMapping(value = "/updatePassword")
	public Object updatePassword(@Validated({ UserUpdate.class }) UserAddModel muser, BindingResult bindingResult) {
		return userService.updatePassword(muser);
	}

}
